<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="org.jawa.admin.util.ParamUtils"%>
<%@ page import="org.jawa.core.util.StringUtils"%>
<%@ page import="org.jawa.core.util.JawaGlobals"%>
<jsp:useBean id="webManager" class="org.jawa.admin.util.WebManager"  />
<% webManager.init(request, response, session, application, out); %>
<%
	String path = request.getContextPath();
%>
<%
	String propName = ParamUtils.getParameter(request, "propName");
	String propValue = ParamUtils.getParameter(request, "propValue", true);
	boolean edit = ParamUtils.getBooleanParameter(request, "edit");
	boolean save = request.getParameter("save") != null;
	boolean delete = ParamUtils.getBooleanParameter(request, "del");

	if (request.getParameter("cancel") != null) {
		response.sendRedirect(path+"/admin/system-properties.jsp");
		return;
	}
	
	//删除系统参数
	if (delete) {
		if (propName != null) {
			JawaGlobals.deleteProperty(propName);
			webManager.logEvent("删除系统参数 "+propName, null);
			response.sendRedirect(path+"/admin/system-properties.jsp?deletesuccess=true");
			return;
		}
	}

	//编辑系统参数
	if (edit) {
		propValue = JawaGlobals.getProperty(propName);
	}
	
	Map<String, String> errors = new HashMap<String, String>();
	
	//保存系统参数
	if (save) {
		if (propName == null || "".equals(propName.trim()) || propName.startsWith("\"")) {
			errors.put("propName", "参数名不可以为空");
		}
		if (propValue == null) {
			errors.put("propValue", "参数值不可以为空");
		} else if (propValue.getBytes().length > 4000) {
			errors.put("propValueLength", "参数值不可以超出4000个字节的长度");
		}
		if (errors.size() == 0) {
			JawaGlobals.setProperty(propName, propValue);
			webManager.logEvent("保存系统参数 "+propName, propName+" = "+propValue);
			response.sendRedirect(path+"/admin/system-properties.jsp?success=true");
			return;
		}
	}

	List<String> properties = JawaGlobals.getPropertyNames();
	try{
		Collections.sort(properties, new Comparator<String>() {
			public int compare(String s1, String s2) {
				return s1.toLowerCase().compareTo(s2.toLowerCase());
			}
		});
	}catch(Exception ex){
		ex.printStackTrace();
	}
	

%>

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>系统属性</title>
		<meta name="pageID" content="1102" />
		<style type="text/css">
			.hidebox {
			    text-overflow : ellipsis;
			    overflow : hidden;
			    white-space : nowrap;
			}
		</style>
	</head>
	<body>
		<div class="row-fluid">
			<ul class="breadcrumb">
				<li>
					您现在的位置:
				</li>
				<li>
					<a href="<%=path%>/admin/index.jsp">服务器</a>
				</li>
				<li>
					<span class="divider">/</span>
				</li>
				<li class="active">
					系统参数
				</li>
			</ul>

			<div class="page-header">
				<h3>
					系统参数
				</h3>
			</div>

			<%
				if (errors.size() > 0) {
			%>

				<div class="alert alert-error fade in">
					<a class="close" data-dismiss="alert" href="#">×</a>
					<strong>错误 － 创建属性失败，请参阅详细内容!</strong>
				</div>

			<%
				} else if ("true".equals(request.getParameter("success"))) {
					//保存系统参数成功
			%>

				<div class="alert alert-success fade in">
					<a class="close" data-dismiss="alert" href="#">×</a>
					<strong>系统参数保存成功!</strong>
				</div>

			<%
				} else if ("true".equals(request.getParameter("deletesuccess"))) {
					//删除系统参数成功
			%>

				<div class="alert alert-success fade in">
					<a class="close" data-dismiss="alert" href="#">×</a>
					<strong>系统参数删除成功!</strong>
				</div>

			<%
				}
			%>

			<form action="<%=path %>/admin/system-properties.jsp" method="post" name="propform" id="propform">
				<input type="hidden" name="edit" id="edit" value="">
				<input type="hidden" name="del" id="del" value="">
				<input type="hidden" name="propName" id="propName" value="">
				<table class="table table-striped  table-bordered ">
					<thead>
						<tr>
							<th>
								参数名
							</th>
							<th>
								参数值
							</th>
							<th style="text-align: center;">
								编辑
							</th>
							<th style="text-align: center;">
								删除
							</th>
						</tr>
					</thead>
					<tbody>
						<%
							int i = 1;
							for (String n : properties) {
								String v = JawaGlobals.getProperty(n);
								v = StringUtils.replace(StringUtils.escapeHTMLTags(v), "\n", "<br>");
						%>
						<tr>
							<td>
								<%=StringUtils.escapeHTMLTags(n)%>
							</td>
							<td >
								<%=("".equals(v) ? "&nbsp;" : v)%>
							</td>
							<td style="text-align: center;">
							    <%
							    	if(!n.startsWith("jawa.")){ 
							    %>
								<a href="#" class="btn btn-mini btn-info"
									onclick="doedit('<%=StringUtils.replace(StringUtils.escapeHTMLTags(n), "'", "''")%>');"><i
									class="icon-edit icon-white"></i> 
								</a>
								<%
							    	}
								%>
							</td>
							<td style="text-align: center;">
							    <%
							    	if(!n.startsWith("jawa.")){ 
							    %>
								<a href="#" class="btn btn-mini btn-danger"
									onclick="return dodelete('<%=StringUtils.replace(StringUtils.escapeHTMLTags(n), "'", "''")%>');"><i
									class="icon-trash icon-white" ></i> 
								</a>
								<%
							    	}
								%>
									
							</td>
						</tr>
						<%
							}
						%>

					</tbody>
				</table>
			</form>

			<div class="page-header">
				<h3>
					<%
						if (edit) {
					%>
					编辑参数
					<%
						} else {
					%>
					添加新参数
					<%
						}
					%>
				</h3>
			</div>
			<a name="edit"></a>
			<form action="<%=path %>/admin/system-properties.jsp" method="post" name="editform" id="editform"
				class="well form-horizontal">
				<div class="control-group <%=errors.containsKey("propName")?"error":""%> ">
					<label class="control-label" for="propNameId">
						参数名:
					</label>
					<div class="controls">
						<%
							if (edit) {
						%>
							<input id="propNameId" type="hidden" name="propName"
								value="<%=StringUtils.escapeHTMLTags(propName)%>" >
							<span class="input-xlarge uneditable-input"><%=StringUtils.escapeHTMLTags(propName)%></span>
						<%
							} else {
						%>
							<input id="propNameId" type="text" class="input-xlarge focused" name="propName" size="40" maxlength="100"
									value="<%=(propName != null ? StringUtils.escapeHTMLTags(propName) : "")%>" placeholder="请输入参数名">
							<%
							}
						%>
								<%
									if (errors.containsKey("propName")) {
								%>
								   <span class="help-inline">		
									<%=errors.get("propName") %>
		  						   </span>
								<%
									}
								%>
					</div>
				</div>
				<div class="control-group <%=errors.containsKey("propValue")?"error":""%> ">
					<label class="control-label" for="propValueId">
						参数值:
					</label>
					<div class="controls">
						<textarea id="propValueId" cols="65" rows="5" style="width:500px;" name="propValue"  placeholder="请输入参数值"><%=(propValue != null ? StringUtils.escapeHTMLTags(propValue) : "")%></textarea>
						<span class="help-inline">
							<%
								if (errors.containsKey("propValue")) {
							%>
								<%=errors.get("propValue") %>
							<%
								} 
							%>
						</span>
					</div>
				</div>
				<div class="form-actions">
						<input type="submit" class="btn btn-primary" name="save"
							value="保存参数">
						<input type="reset" class="btn" name="cancel" value="重设">
				</div>
			</form>
		</div>
		<script src="<%= path %>/scripts/bootstrap.js"></script>
		<script src="<%= path %>/scripts/jquery.validate.js"></script>
		<script>
				function doedit(propName) {
				    document.getElementById('propName').value = propName;
					document.getElementById('edit').value = 'true';
					document.getElementById('propform').action = document.getElementById('propform').action + '#edit';
					document.getElementById('propform').submit();
				}
				function dodelete(propName) {
				    var dodelete = confirm('您确定要删除当前参数吗?');
				    if (dodelete) {
				    	 document.getElementById('propName').value = propName;
				    	 document.getElementById('del').value = 'true';
				    	 document.getElementById('propform').submit();
				         return true;
				    }
				    else {
				        return false;
				    }
				}

				$().ready(function() {
					$("#editform").validate({
						errorClass: "validate-error",
						validClass: "validate-success",
						errorElement:'span',
						errorPlacement: function(error, element) {
							element.parent().parent().addClass('error');
							error.appendTo(element.parent());
						},
						success: function(label) {
							label.html('√');
							label.parent().parent().removeClass("error");
							label.parent().parent().addClass("success");
						},
						rules: {
							propName: {
								required:true
							},
							propValue: {
								required:true
							}
						},
						messages: {
							propName: {
							  required:'请输入参数名称!'
							},
							propValue: {
								required:'请输入参数值!'
							}						
						}
					});
				});
				
		</script>
	</body>
</html>